package com.camera.yl;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.media.MediaCodec;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.camera.base.bean.CameraRecordResult;
import com.camera.base.BaseApp;
import com.camera.base.WebSocketEventHandler;
import com.camera.base.bean.CheckAppUpgradeResponse;
import com.camera.base.bean.YLBeanUtil;
import com.camera.base.inerface.FullEventListener;
import com.camera.base.inerface.ICPInterface;
import com.camera.base.inerface.YLWebrtcInterface;
import com.camera.base.net.NetworkImpl;
import com.camera.base.net.util.HttpCallback;
import com.camera.base.util.RtspMMKVUtil;
import com.camera.base.util.YLCameraConfig;
import com.camera.base.v.models.SystemNavBarViewModel;
import com.camera.base.wbmodels.WebModel;
import com.camera.routemodule.ARoutImp;
import com.camera.routemodule.RouterPath;
import com.camera.routemodule.routeinterface.BDLocationInterface;
import com.camera.routemodule.routeinterface.BDLocationListener;
import com.camera.routemodule.routeinterface.GBAppInterface;
import com.camera.routemodule.routeinterface.IMAppInterface;
import com.camera.routemodule.routeinterface.RouterUtil;
import com.camera.routemodule.routeinterface.RtspInterface;
import com.camera.routemodule.routeinterface.YLWebSocketInterface;
import com.camera.yl.util.CameraPopManager;
import com.camera.yl.util.CrashHandler;
import com.camera.yl.views.CameraManager;

import javax.inject.Inject;
import dagger.hilt.android.HiltAndroidApp;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;

@HiltAndroidApp
public class ICPAPP extends Application {
    private final static String TAG = ICPAPP.class.getSimpleName();
    String filaPath,preFilePath;
    YLWebSocketInterface mYLWebSocketInterface;
    @Inject
    @Nullable
    ICPInterface mICPInterface;
    IMAppInterface mIMAppInterface;
    @Inject
    @Nullable
    YLWebrtcInterface mYLWebrtcInterface;

    GBAppInterface mGBAppInterface;
    RtspInterface mRtspInterface;
    public SystemNavBarViewModel mSystemNavBarViewModel;
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate: ICPAPP" );
        //IM库里有新的进程 导致Application 会多次执行 通过pid确定是否是主APP初始化
        if (getPackageName() != null && getPackageName().equals(getCurrentProcessName(this))) {//android.os.Process.myPid()
            ARoutImp.INSTANCE.initA(this);
            //初始化IM 对讲模块
            mIMAppInterface = RouterUtil.INSTANCE.getIMManagerImpl();
            //初始化国标模块
            mGBAppInterface = RouterUtil.INSTANCE.getGBManagerImpl();
            //初始化websocket模块
            mYLWebSocketInterface = RouterUtil.INSTANCE.getWebSocketImpl();
            //初始化APP类系统导航栏的UI 的ViewModel
            mSystemNavBarViewModel = BaseApp.mSystemNavBarViewModel;
            //初始化百度定位模块  后续需要修改成可选择的(百度,高得,机身)
            if (RouterUtil.INSTANCE.getDBLocationImpl() != null) {
                RouterUtil.INSTANCE.getDBLocationImpl().init(this);
            }
            //崩溃抓取日志 模块
            CrashHandler.getInstance().init(this);//
            //初始化摄像头模块
            if (mICPInterface != null){
                CameraManager.INSTANCE.initCameraServer(mICPInterface);//赋予服务
                CameraManager.INSTANCE.setSystemNavBarViewModel(mSystemNavBarViewModel);
            }
            //摄像头模块赋予 Websocket 模块
            if(mYLWebSocketInterface != null){
                mYLWebrtcInterface.init(this);
                mYLWebSocketInterface.addEventHandler(ylEventHandler);
                CameraManager.INSTANCE.setWebSocketServer(mYLWebSocketInterface);//赋予服务  对外设置websocket接口  不需要的话可以去掉不理
            }
            //IM对讲模块赋予 Websocket 模块
            if (mIMAppInterface != null){
                mIMAppInterface.init(this,mICPInterface);
                mIMAppInterface.setWebSocketServer(mYLWebSocketInterface);
            }
            //国标28181模块赋予 Websocket 模块
            if (mGBAppInterface != null){
                mGBAppInterface.init(this);
                mGBAppInterface.setWebSocketServer(mYLWebSocketInterface);
            }
            //初始化RTSP模块
            mRtspInterface = RouterUtil.INSTANCE.getRtspServerImpl();
            if (mRtspInterface != null){
                if (RtspMMKVUtil.INSTANCE.getRtspServerEnabled()) {
                    //开启RTSP 服务端 等待拉流
                    mRtspInterface.startRtspServer(RtspMMKVUtil.INSTANCE.getRtspServerPort());
                }
            }

        }
    }

    WebSocketEventHandler ylEventHandler = new WebSocketEventHandler() {
        @Override
        public void onWebSocketMeesage(@NonNull WebModel mWebModel) {
            if (mWebModel.getFoctory().equals("camera")) {
                if (mWebModel.getCameraWebModel().getAbility().equals("set_mark")) {
                    BaseApp.ylBean.localCamera.setCamera_marks(mWebModel.getCameraWebModel().getCameraMarks());
                    YLCameraConfig.setCameraMarks(mWebModel.getCameraWebModel().getCameraMarks());
                    YLBeanUtil.Companion.configYLBean(BaseApp.ylBean,ICPAPP.this);
                    BaseApp.ylBean = YLBeanUtil.Companion.getYLBean(ICPAPP.this);
                    CameraManager.INSTANCE.configMarksAll();
                }else if (mWebModel.getCameraWebModel().getAbility().equals("set_mark_txt")) {
//                    CameraManager.INSTANCE.setMarksConfigTxt(mWebModel.getCameraWebModel().getCameraMarkTxts());
                }else if (mWebModel.getCameraWebModel().getAbility().equals("set_camera_param")) {
                    mICPInterface.setSubStreamChoose(mWebModel.getCameraWebModel().getCameraParamConfig().getSubstreamChoose());
//                    mICPInterface.setSubStreamRateChoose(mWebModel.getCameraWebModel().getCameraParamConfig().getSubstreamRateChoose());
                }else if (mWebModel.getCameraWebModel().getAbility().equals("show_preview")) {
                    int width = mWebModel.getCameraWebModel().getPreviewConfig().getWidth();
                    int height = mWebModel.getCameraWebModel().getPreviewConfig().getHeight();
                    int x = mWebModel.getCameraWebModel().getPreviewConfig().getX();
                    int y = mWebModel.getCameraWebModel().getPreviewConfig().getY();
                    boolean isProportion = mWebModel.getCameraWebModel().getPreviewConfig().isProportion();
//                    CameraPopManager.INSTANCE.startFloatingCameraFromBackground(ICPAPP.this);
                    CameraManager.INSTANCE.openCamera();
//                    CameraManager.INSTANCE.startCameraPreview(ICPAPP.this,mWebModel.getCameraWebModel().getPreviewConfig().getMode().equals("Full"),width,height,x,y,isProportion);
                    CameraManager.INSTANCE.startCameraPreview(ICPAPP.this,false,100,100,20,20,true);
                }else if (mWebModel.getCameraWebModel().getAbility().equals("show_im_preview")) {
                    CameraPopManager.INSTANCE.startFloatingCameraFromBackground(ICPAPP.this);
                }else if(mWebModel.getCameraWebModel().getAbility().equals("dis_preview")){
                    CameraManager.INSTANCE.disCameraPreview(ICPAPP.this);
                }else if(mWebModel.getCameraWebModel().getAbility().equals("start_record")){
                    if (mWebModel.getCameraWebModel().getRecordConfig().isGetPrepath()){
                        filaPath = mWebModel.getCameraWebModel().getRecordConfig().getPath();
                        preFilePath = mWebModel.getCameraWebModel().getRecordConfig().getPrepath();
                        CameraManager.INSTANCE.setFilaPath(filaPath);
                        CameraManager.INSTANCE.setPreFilePath(preFilePath);
                        CameraManager.INSTANCE.startRecord(new Function1<CameraRecordResult, Unit>() {
                            @Override
                            public Unit invoke(CameraRecordResult cameraRecordResult) {

                                return null;
                            }
                        });
                    }else{
                        CameraManager.INSTANCE.setPreFilePath(preFilePath);
                        preFilePath = mWebModel.getCameraWebModel().getRecordConfig().getPrepath();
                    }
                }else if(mWebModel.getCameraWebModel().getAbility().equals("stop_record")){
                    mICPInterface.stopRecord();
                }
            }else if(mWebModel.getFoctory().equals("im")){
                mIMAppInterface.getWebSocketApi(mWebModel);
            }
        }
    };
    private String getCurrentProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }
}
